/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.StReprografiaDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.StReprografia;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *
 * @author CAR
 */
public class listaStReprografia extends HttpServlet {

    public listaStReprografia() {
            super();

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //printwriter to send the JSON response back
        PrintWriter out = response.getWriter();
        //set content type
        response.setContentType("text/html");

        //create a new JSON array to send the list of items
        JSONArray arrayObj = new JSONArray();


        //get arraylist of customers based on the request 
        StReprografiaDAO stReprografiaDAO = new StReprografiaDAO();
        ArrayList<StReprografia> stReprografiaList = stReprografiaDAO.getReprografias();

        //loop thru the array list to populate the JSON array
        for (int i = 0; i < stReprografiaList.size(); i++) {

            //get customer Object
            StReprografia stReprografia = stReprografiaList.get(i);
            //this creates a JSON object from bean object
            JSONObject stReprografiaObj = JSONObject.fromObject(stReprografia);
            //add to array list
            arrayObj.add(stReprografiaObj);
        }

        //Create a JSON object to wrap your JSOn array and provide the root element items
        JSONObject myObj = new JSONObject();
        //sets success to true
        myObj.put("success", true);
        //set the JSON root to items
        myObj.put("reprografias", arrayObj);

        //convert the JSON object to string and send the response back
        out.println(myObj.toString());
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
}
